package vn.aptech.leaveManagementSystem.view.personnel;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;

import vn.aptech.leaveManagementSystem.business.Validate;
import vn.aptech.leaveManagementSystem.business.dto.Application;
import vn.aptech.leaveManagementSystem.business.model.Applications;
import vn.aptech.leaveManagementSystem.business.model.MyModel;
import vn.aptech.leaveManagementSystem.business.parse.MyParse;

import com.toedter.calendar.JCalendar;
import com.toedter.calendar.JDateChooser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

public class DemoCreateNewApplicationForPersonnel extends JFrame{

	private JDateChooser dcStartDate;
	private JDateChooser dcEndDate;
	private JTextField txtfContact;
	private JTextArea txtaReason;
	private DemoPersonnelMainForm mainform;
	private int idEmpParameter;
	private JButton btnSubmit;
	private JButton btnCancel;
	private MyModel mymodel;

	public DemoCreateNewApplicationForPersonnel(DemoPersonnelMainForm mainform,int idEmp) {
		this.mainform=mainform;
		this.idEmpParameter=idEmp;
		initGUI();
		initData();
		initAction();
	}

	private void initAction() {
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//-----------------------------------------------------------------------
				//Validate days leave
				toBack();
				if (dcStartDate.getDate()==null)
				{
					JOptionPane.showMessageDialog(null, "StartDate not choose!");
					return;
				}
				
				if (dcEndDate.getDate()==null)
				{
					JOptionPane.showMessageDialog(null, "EndDate not choose!");
					return;
				}
				
				Validate vld=new Validate();
				int leaveBalance = 0;
				try {
					leaveBalance = mymodel.employees.getLeaveBalanceInThisTime(idEmpParameter);
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				if (!vld.testAcceptDatesLeave(dcStartDate.getDate(),dcEndDate.getDate(),leaveBalance))
				{
					return;
				}
				
				//-----------------------------------------------------------------------
				Application app=new Application();
				app.setEmpID(idEmpParameter);
				app.setStartDate(MyParse.toSqlDate(dcStartDate.getDate()));
				app.setEndDate(MyParse.toSqlDate(dcEndDate.getDate()));
				app.setReason(txtaReason.getText());
				app.setContact(txtfContact.getText());
				app.setStatus(0);
				try {
					mymodel.applications.addApplication(app);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				close();
			}
		});
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}

	private void initData() {
		mymodel=new MyModel();
	}


	private void initGUI() {
		setTitle("Create New Application");
		setBounds(100, 100, 518, 346);
		setCloseForm();
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		dcStartDate = new JDateChooser();
		dcStartDate.setBounds(106, 34, 358, 20);
		getContentPane().add(dcStartDate);
		
		JLabel lblStartDate = new JLabel("Start Date:");
		lblStartDate.setBounds(10, 34, 86, 14);
		getContentPane().add(lblStartDate);
		
		dcEndDate = new JDateChooser();
		dcEndDate.setBounds(106, 63, 358, 20);
		getContentPane().add(dcEndDate);
		
		txtaReason = new JTextArea();
		txtaReason.setBounds(106, 94, 358, 122);
		getContentPane().add(txtaReason);
		
		txtfContact = new JTextField();
		txtfContact.setBounds(106, 227, 358, 20);
		getContentPane().add(txtfContact);
		txtfContact.setColumns(10);
		
		JLabel lblEndDate = new JLabel("End Date:");
		lblEndDate.setBounds(10, 63, 86, 14);
		getContentPane().add(lblEndDate);
		
		JLabel lblReason = new JLabel("Reason:");
		lblReason.setBounds(10, 96, 86, 14);
		getContentPane().add(lblReason);
		
		JLabel lblContact = new JLabel("Contact:");
		lblContact.setBounds(10, 230, 86, 14);
		getContentPane().add(lblContact);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setIcon(new ImageIcon("icons/gif/16x16/OK.gif"));
		btnSubmit.setBounds(160, 258, 102, 23);
		getContentPane().add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		btnCancel.setBounds(272, 258, 107, 23);
		getContentPane().add(btnCancel);
	}
	
	public void setCloseForm()
	{
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrame jframe=(JFrame) e.getSource();
				mainform.setEnabled(true);
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
	}
	
	public void close()
	{
		toBack();
		dispose();
		mainform.setEnabled(true);
		mainform.UpdateLastedApplication();
	}
}


